/*
 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
 *
 * Copyright (C) 2002-2007 Aleph One Ltd.
 *   for Toby Churchill Ltd and Brightstar Engineering
 *
 * Created by Charles Manning <charles@aleph1.co.uk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

/*
 * yaffscfg2k.c  The configuration for the "direct" use of yaffs.
 *
 * This file is intended to be modified to your requirements.
 * There is no need to redistribute this file.
 */

#include "yaffscfg.h"
#include "yaffsfs.h"
//#include "yaffs_fileem2k.h"
//#include "yaffs_nandemul2k.h"
//#include "yaffs_norif1.h"
#include "yaffs_trace.h"

#include <errno.h>


unsigned yaffs_traceMask = 

	YAFFS_TRACE_SCAN |  
	YAFFS_TRACE_GC |
	YAFFS_TRACE_ERASE | 
	YAFFS_TRACE_ERROR | 
	YAFFS_TRACE_TRACING | 
	YAFFS_TRACE_ALLOCATE | 
	YAFFS_TRACE_BAD_BLOCKS |
	YAFFS_TRACE_VERIFY | 
	
	0;
        

static int yaffsfs_lastError;

void yaffsfs_SetError(int err)
{
	//Do whatever to set error
	yaffsfs_lastError = err;
}


int yaffsfs_GetLastError(void)
{
	return yaffsfs_lastError;
}

void yaffsfs_Lock(void)
{
}

void yaffsfs_Unlock(void)
{
}

__u32 yaffsfs_CurrentTime(void)
{
	return 0;
}


static int yaffs_kill_alloc = 0;
static size_t total_malloced = 0;
static size_t malloc_limit = 0 & 6000000;

void *yaffs_malloc(size_t size)
{
	void * this;
	if(yaffs_kill_alloc)
		return NULL;
	if(malloc_limit && malloc_limit <(total_malloced + size) )
		return NULL;

	this = xmalloc_bytes(size);
	if(this)
		total_malloced += size;
	return this;
}

void yaffs_free(void *ptr)
{
	xfree(ptr);
}

void yaffsfs_LocalInitialisation(void)
{
	// Define locking semaphore.
}

// Configuration for:
// /ram  2MB ramdisk
// /boot 2MB boot disk (flash)
// /flash 14MB flash disk (flash)
// NB Though /boot and /flash occupy the same physical device they
// are still disticnt "yaffs_Devices. You may think of these as "partitions"
// using non-overlapping areas in the same device.
// 

//#include "yaffs_ramdisk.h"
//#include "yaffs_flashif.h"
//#include "yaffs_flashif2.h"
//#include "yaffs_nandemul2k.h"
#include "goldfish_Chunk_API.h"

static yaffs_Device flashDev;
static yaffs_Device userDev;
static yaffs_Device cacheDev;

static yaffsfs_DeviceConfiguration yaffsfs_config[] = {
	{ "/system", &flashDev},
	{ "/userdata", &userDev},
	{ "/cache", &cacheDev},
	{(void *)0,(void *)0} /* Null entry to terminate list */
};


int yaffs_StartUp(void)
{
	// Stuff to configure YAFFS
	// Stuff to initialise anything special (eg lock semaphore).
	yaffsfs_LocalInitialisation();
	


	// /yaffs2 
	// Set this puppy up to use
	// the file emulation space as
	// 2kpage/64chunk per block
	//
	memset(&flashDev,0,sizeof(flashDev));
	flashDev.param.name="system";
	flashDev.param.totalBytesPerChunk = 2048;//Number of bytes in data area
	flashDev.param.spareBytesPerChunk = 64; 
	flashDev.param.nChunksPerBlock = 64;//一个块有131072Bytes，也就是128K。这样就是64
	flashDev.param.nReservedBlocks = 5;
	flashDev.param.inbandTags = 0;//whether tags should be stored in data area
	flashDev.param.startBlock = 0;
	flashDev.param.endBlock = 511;//一共有512个128K的block
	flashDev.param.isYaffs2 = 1;
	flashDev.param.useNANDECC=1;
	flashDev.param.noTagsECC = 1;
//	flashDev.param.wideTnodesDisabled=0;
	flashDev.param.refreshPeriod = 10000;
	flashDev.param.nShortOpCaches = 10; // Use caches
	flashDev.context = (void *) 0;	// Used to identify the device in fstat.
	flashDev.param.writeChunkWithTagsToNAND = Goldfish_Chunk_WriteChunk;
	flashDev.param.readChunkWithTagsFromNAND = Goldfish_Chunk_ReadChunk;
	flashDev.param.eraseBlockInNAND = Goldfish_Chunk_EreaseBlock;
	flashDev.param.initialiseNAND = Goldfish_Chunk_Initialize;
	flashDev.param.markNANDBlockBad = Goldfish_Chunk_MarkBadBlock;
	flashDev.param.queryNANDBlock = Goldfish_Chunk_QueryBlock;

	memset(&userDev,0,sizeof(userDev));
	userDev.param.name="userdata";
	userDev.param.totalBytesPerChunk = 2048;//Number of bytes in data area
	userDev.param.spareBytesPerChunk = 64; 
	userDev.param.nChunksPerBlock = 64;//一个块有131072Bytes，也就是128K。这样就是64
	userDev.param.nReservedBlocks = 5;
	userDev.param.inbandTags = 0;//whether tags should be stored in data area
	userDev.param.startBlock = 0;
	userDev.param.endBlock = 511;//一共有512个128K的block
	userDev.param.isYaffs2 = 1;
	userDev.param.useNANDECC=1;
	userDev.param.noTagsECC = 1;
//	flashDev.param.wideTnodesDisabled=0;
	userDev.param.refreshPeriod = 10000;
	userDev.param.nShortOpCaches = 10; // Use caches
	userDev.context = (void *) 1;	// Used to identify the device in fstat.
	userDev.param.writeChunkWithTagsToNAND = Goldfish_Chunk_WriteChunk;
	userDev.param.readChunkWithTagsFromNAND = Goldfish_Chunk_ReadChunk;
	userDev.param.eraseBlockInNAND = Goldfish_Chunk_EreaseBlock;
	userDev.param.initialiseNAND = Goldfish_Chunk_Initialize;
	userDev.param.markNANDBlockBad = Goldfish_Chunk_MarkBadBlock;
	userDev.param.queryNANDBlock = Goldfish_Chunk_QueryBlock;
	
	memset(&cacheDev,0,sizeof(cacheDev));
	cacheDev.param.name="cache";
	cacheDev.param.totalBytesPerChunk = 2048;//Number of bytes in data area
	cacheDev.param.spareBytesPerChunk = 64; 
	cacheDev.param.nChunksPerBlock = 64;//一个块有131072Bytes，也就是128K。这样就是64
	cacheDev.param.nReservedBlocks = 5;
	cacheDev.param.inbandTags = 0;//whether tags should be stored in data area
	cacheDev.param.startBlock = 0;
	cacheDev.param.endBlock = 511;//一共有512个128K的block
	cacheDev.param.isYaffs2 = 1;
	cacheDev.param.useNANDECC=1;
	cacheDev.param.noTagsECC = 1;
//	flashDev.param.wideTnodesDisabled=0;
	cacheDev.param.refreshPeriod = 10000;
	cacheDev.param.nShortOpCaches = 10; // Use caches
	cacheDev.context = (void *) 2;	// Used to identify the device in fstat.
	cacheDev.param.writeChunkWithTagsToNAND = Goldfish_Chunk_WriteChunk;
	cacheDev.param.readChunkWithTagsFromNAND = Goldfish_Chunk_ReadChunk;
	cacheDev.param.eraseBlockInNAND = Goldfish_Chunk_EreaseBlock;
	cacheDev.param.initialiseNAND = Goldfish_Chunk_Initialize;
	cacheDev.param.markNANDBlockBad = Goldfish_Chunk_MarkBadBlock;
	cacheDev.param.queryNANDBlock = Goldfish_Chunk_QueryBlock;
	
	yaffs_initialise(yaffsfs_config);
	return 0;
}



